<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>导出</el-breadcrumb-item>
        <el-breadcrumb-item>导出下载</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button v-if="scope.row.state==1"
            icon="el-icon-primary"
            type="text"
            size="small"
            @click.stop="download(scope.row)"
            >  下载 </el-button>
            <span v-if="scope.row.state==0">生成中，请等待~</span>
            <span v-if="scope.row.state==2">文件过期</span>
            <span v-if="scope.row.state==3">导出失败</span>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { exportRecord as baseURL } from "@/api/cat/baseUrls";
export default {
    data(){
        return{
            loading:false,
            page: {
                total: 0,
                pageSize: 10
            },
            type: true,
            data: [],
            params: {},
            form: {},
            default: {
            },  
            option: {
                addBtn: false,
                editBtn: false,
                viewBtn: false,
                delBtn: false,
                excelBtn: false,
                border: true,
                menu: true,
                column: [
                    {
                        label: "搜索日期",
                        prop: "statistics_date_search",
                        type: "datetimerange",
                        search: true,
                        hide:true
                    },
                    {
                        label: "编号id",
                        prop: "id",
                        type: "input",
                        width: "80px"
                    },
                    {
                        label: "文件路径",
                        prop: "route",
                        search: true,
                        type: "input",
                        width: "300px"
                    },
                    {
                        label: "导出条件",
                        prop: "export_condition",
                        type: "input",
                        width: "300px"
                    },
                    {
                        label: "预计耗时(秒)",
                        prop: "preset_time",
                        type: "input",
                        width: "100px"
                    },
                    {
                        label: "导出时间",
                        prop: "create_time",
                        type: "date",
                        width: "150px"
                    },
                    {
                        label: "导出人",
                        prop: "export_user_name",
                        search: true,
                        type: "input"
                    },
                    {
                        label: "文件状态",
                        prop: "state",
                        type: "select",
                        search: true,
                        dicData: [
                            {
                                label: "生成中…",
                                value: 0,
                            },
                            {
                                label: "已完成",
                                value: 1,
                            },
                            {
                                label: "已过期",
                                value: 2,
                            },
                            {
                                label: "失败",
                                value: 3,
                            },
                        ],
                    },
                    {
                        label: "文件下载链接",
                        prop: "file_download_path",
                        type: "input",
                        hide: true,
                        width: "400px"
                    },
                    {
                        label: "文件生成失败日志",
                        prop: "error_msg",
                        type: "input",
                        hide: true,
                        width: "400px"
                    },
                ]
            }
        }
    },
    methods:{
        /**获取列表 */
        async getList() {
            this.loading = true;
            //清空表格列表
            this.data = [];
            //发送请求获取数据
            let response = await common(
                baseURL.getExportRecordList,
                { ...this.params, ...this.default },
                this.page,
                "POST"
            );
            this.page = response.page;
            this.data = response.data;
            this.loading = false;
        },

        /**下载 */
        download(row){
            window.location.href = row.file_download_path;
        },
        
        /**刷新 */
        refreshChange() {
            this.getList();
        },

        /**搜索 */
        searchChange(params, done) {
            if(params.statistics_date_search){
                params.startTime = params.statistics_date_search[0];
                params.endTime = params.statistics_date_search[1];
            }

            this.params = params;
            this.page.currentPage = 1;
            this.getList();
            if (done) done();
        }
    }
}
</script>

<style>

</style>